﻿using D.UtilCore;
using D.UtilCore.Dtos;
using D.UtilCore.Filters;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using CozeDto.ProjectDtos;
using CozeDomain.IServices;
using D.UtilFreesqlOrm;

namespace CozeWebApi
{
    /// <summary>
    /// 项目模型模块
    /// </summary>
    [Route("/api/[controller]/[action]")]
    [Authorize]
    public class ProjectController : BaseApiController
    {
        private readonly IProjectService _projectService;
        public ProjectController(IProjectService projectService)
        {
            _projectService = projectService;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="reqAddProject"></param>
        /// <returns></returns>
        [HttpPost, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<int>> Add([FromBody] ReqAddProject reqAddProject)
        {
            reqAddProject.CreateUser = Account;
            return await _projectService.AddProjectAsync(reqAddProject);
        }

        /// <summary>
        /// 删除，多个逗号隔开
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{ids}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        [UnitOfWork]
        public async Task<ApiResult<int>> Delete(string ids)
        {
            var idsArray = ids.Split(",");
            return await _projectService.DeleteProjectAsync(idsArray.ToList());
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="reqUpdateProject"></param>
        /// <returns></returns>
        [HttpPut("{id}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<int>> Update(string id, [FromBody] ReqUpdateProject reqUpdateProject)
        {
            reqUpdateProject.Id = id;
            reqUpdateProject.UpdateUser = Account;
            return await _projectService.UpdateProjectAsync(reqUpdateProject);
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<RepProjectInfo>> Get(string? id)
        {
            if (string.IsNullOrEmpty(id))
            {
                id = LoginId;
            }
            return await _projectService.GetProjectAsync(id);
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <returns></returns>
        [HttpGet, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<List<RepProjectInfo>>> List()
        {
            return await _projectService.QueryProjectListAsync();
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <returns></returns>
        [HttpPost, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<PageModel<RepProjectInfo>>> Pages([FromBody] ReqQueryProjectPage reqQueryProjectPage)
        {
            return await _projectService.QueryProjectPagesAsync(reqQueryProjectPage);
        }
    }
}
